<template>
	<view class="page-ranking" :style="'background-image: url('+ config.index_bc + ')'">
		<view class="search-box">
			<view style="flex: 1;">
				<u-search v-model="keyWords" :show-action="false" height="62" color="#fff" search-icon-color="#fff" bg-color="#06B2FF"
					border-color="#fff" placeholder="请输入人名" placeholder-color="#fff" @search="getList"></u-search>
			</view>
		</view>
		<view class="c-info-box" :style="'background-color: ' + config.index_border_color" v-if="currentData.rowNo !== 0">
			<view class="current-box">
				<view class="number">
					{{currentData.rowNo}}
				</view>
				<view class="head-box" :style="'border-color:'+'#4EB3A3'">
					<u-avatar :src="currentData.headImg" :size="88"></u-avatar>
				</view>
				<view style="margin-left: 15rpx; flex: 1">
					<view class="name">
						<text style="margin-right: 20rpx; color: #FFFFFF;">{{currentData.name}}</text>
						<text style="color: #FFFFFF;">{{currentData.area}}</text>
					</view>
					<!-- <view class="company" style="color: #FFFFFF; font-size: 20rpx;">
						{{currentData.companyName}}
					</view> -->
				</view>
				<view class="steps">
					{{currentData.steps}}
				</view>
				<view class="like" @click="tapLike(currentData)">
					<view class="count light" :class="currentData.isLike ? 'red' : ''">
						{{currentData.like}}
					</view>
					<view class="icon-box">
						<image class="icon" :src="currentData.isLike ? config.like1 : config.like0" mode="scaleToFill"></image>
					</view>
				</view>
			</view>
			<view class="user-count">
				参与人数：{{currentData.persionNum || 0}}
			</view>
		</view>
		<view class="btn-box">
			<view class="btn" :class="listType === 1 ? 'current' : ''" @click="listTypeChange(1)">
				今日排名
			</view>
			<view class="btn" :class="listType === 2 ? 'current' : ''" @click="listTypeChange(2)">
				累计排名
			</view>
		</view>
		<scroll-view scroll-y class="list-box" @scrolltolower="loadMore" refresher-enabled	:refresher-triggered="triggered" @refresherpulling="onPulling"
            @refresherrefresh="onRefresh" @refresherrestore="onRestore">
			
			<template v-for="(item, index) in list">
				<view class="top-three-item" :key="index" v-if="index < 3">
					<view class="top-number" :style="'background-image: url('+ config[topIconList[index]] + ')'">
						{{index + 1}}
					</view>
					<view class="number"></view>
					<view class="head-box" :style="'border-color:'+'#4EB3A3'">
						<u-avatar :src="item.headImg" :size="80"></u-avatar>
					</view>
					<view class="item-name" style="color: #303133;">
						<view class="name">
							<text style="margin-right: 20rpx; color: #303133;">{{item.realName}}</text>
							<text style="color: #303133;">{{item.area}}</text>
						</view>
						<!-- <view class="company" style="color: #303133; font-size: 20rpx;">
							{{item.companyName}}
						</view> -->
					</view>
					<view class="item-steps">
						{{item.step}}
					</view>
					<view class="like" @click="tapLike(item)">
						<view class="count" :class="item.isLike ? 'red' : ''">
							{{item.like}}
						</view>
						<view class="icon-box">
							<image class="icon" :src="item.isLike ? config.like1 : config.like0" mode="scaleToFill"></image>
						</view>
					</view>
				</view>
				<view class="item" :key="index" v-else>
					<view class="item-number">
						{{index + 1}}
					</view>
					<view class="head-box" :style="'border-color:'+'#4EB3A3'">
						<u-avatar :src="item.headImg" :size="80"></u-avatar>
					</view>
					<view class="item-name" style="color: #303133;">
						<view class="name">
							<text style="margin-right: 20rpx; color: #303133;">{{item.realName}}</text>
							<text style="color: #303133;">{{item.area}}</text>
						</view>
						<!-- <view class="company" style="color: #303133; font-size: 20rpx;">
							{{item.companyName}}
						</view> -->
					</view>
					<view class="item-steps">
						{{item.step}}
					</view>
					<view class="like" @click="tapLike(item)">
						<view class="count" :class="item.isLike ? 'red' : ''">
							{{item.like}}
						</view>
						<view class="icon-box">
							<image class="icon" :src="item.isLike ? config.like1 : config.like0" mode="scaleToFill"></image>
						</view>
					</view>
				</view>
			</template>
			<view class="load-more-box" style="margin-top: 40rpx;">
				<u-loadmore :status="status" />
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				config: {
					index_bc: '',
					index_border_color: '',
					like0: '',
					like1: '',
					top_one_icon: '',
					top_two_icon: '',
					top_three_icon: ''
				},
				currentData: {
					uid: '',
					rowNo: 0,
					name: '',
					steps: 0,
					like: 0,
					isLike: false,
					headImg: '',
					persionNum: 0,
					companyName: '',
					area: ''
				},
				keyWords: '',
				topIconList: ['top_one_icon', 'top_two_icon', 'top_three_icon'],
				listType: 1,
				list: [],
				status: 'loadmore',
				pageIndex: 1,
				triggered: false,
				aid: ''
			}
		},
		onLoad(e) {
			this.aid = e.aid;
			this.getList();
			this._freshing = false;
			uni.setNavigationBarTitle({
			    title: e.name
			});
		},
		watch: {
			keyWords(value) {
				if(value === '') {
					this.pageIndex = 1
					this.getList()
				}
			}
		},
		methods: {
			listTypeChange(type) {
				this.listType = type;
				this.pageIndex = 1;
				this.getList()
			},
			async getList() {
				const reqData = {
					aid: this.aid,
					uid: this.userInfo.uid,
					pageIndex: 1,
					pageSize: 20,
					tag: this.listType
				}
				if(this.keyWords) reqData.userName = this.keyWords
				const [res, err] = await this.$get('xcx/findAreaUserRangeList', reqData);
				if(err) return
				const range = res.data.range
				if(range) {
					this.currentData.rowNo = range.rowNo;
					this.currentData.name = range.realName;
					this.currentData.steps = range.step;
					this.currentData.uid = range.uid
					this.currentData.like = range.like;
					this.currentData.isLike = range.isLike;
					this.currentData.headImg = range.headImg;
					this.currentData.persionNum = range.persionNum
					this.currentData.companyName = range.companyName
					this.currentData.area = range.area
				}
				this.list = res.data.list;
				if(res.data.list.length < 20) this.status = 'nomore'
				else this.status = 'loadmore'
			},
			async loadMore() {
				if(this.status === 'nomore') return
				this.pageIndex ++;
				this.status = 'loading'
				const reqData = {
					aid: this.aid,
					uid: this.userInfo.uid,
					pageIndex: 1,
					pageSize: 20,
					tag: this.listType
				}
				if(this.keyWords) reqData.userName = this.keyWords
				const [res, err] = await this.$get('xcx/findAreaUserRangeList', reqData);
				if(err) return
				this.list = [...this.list, ...res.data.list];
				if(res.data.list.length < 10) this.status = 'nomore'
				else this.status = 'loadmore'
			},
			onPulling(e) {
				this.triggered = true;
			},
			async onRefresh() {
				if (this._freshing) return;
				this._freshing = true;
				this.pageIndex = 1;
				await this.getList()
				this.triggered = false;
				this._freshing = false;
			},
			onRestore() {
				this.triggered = 'restore'; // 需要重置
			},
			async tapLike(item) {
				const reqData = {
					uid: this.userInfo.uid,
					likeId: item.uid,
					tag: 1,
					cancelTag: item.isLike ? 2 : 1
				}
				const [res, err] = await this.$post('xcx/pointLike', reqData);
				if(err) {
					uni.showToast({
						title: '点赞/取消点赞失败',
						icon: 'none'
					})
					return
				}
				item.isLike = !item.isLike;
				if(item.isLike) item.like = item.like + 1
				else item.like = item.like - 1
			}
		}
	}
</script>

<style lang="scss">
	.page-ranking {
		width: 100%;
		height: 100%;
		background-size: 44% auto;
		background-repeat: no-repeat;
		background-position: 562rpx 215rpx;
		
		.search-box {
			width: 100%;
			height: 126rpx;
			padding: 0rpx 38rpx 0rpx 38rpx;
			display: flex;
			background-color: #06B2FF;
			align-items: center;
		}
		
		.list-box {
			width: 100%;
			height: calc(100% - 44rpx - 124rpx - 124rpx - 126rpx - 2rpx);
			background-color: rgba(255,255,255,.5);
		}
		
		.c-info-box {
			border-bottom: 2rpx solid #E8EAED;
			padding-bottom: 10rpx;
			
			.current-box {
				border: none;
			}
			
			.user-count {
				height: 34rpx;
				padding: 0 30rpx;
				text-align: right;
				color: #FFFFFF;
				font-size: 24rpx;
			}
		}
		
		.btn-box {
			width: 100%;
			height: 124rpx;
			display: flex;
			justify-content: space-around;
			align-items: center;
			background-color: rgba(255, 255, 255, .5);
			
			.btn {
				width: 244rpx;
				height: 76rpx;
				// background: #E5404F;
				background-color: #06B2FF;
				border-radius: 100rpx;
				line-height: 76rpx;
				text-align: center;
				color: #FFFFFF;
				font-size: 28rpx;
				
				&.current {
					background-color: #E5404F;
				}
			}
		}
		
		.current-box, .top-three-item, .item {
			width: 100%;
			height: 124rpx;
			opacity: 1;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 30rpx;
			box-sizing: border-box;
			border-bottom: 2rpx solid #E8EAED;
			
			.number {
				width: 40rpx;
				font-size: 28rpx;
				color: #FFFFFF;
			}
			
			.item-number {
				width: 40rpx;
				font-size: 28rpx;
				color: #C4CAD4;
			}
			
			.head-box {
				width: 88rpx;
				height: 88rpx;
				background: rgba(0, 0, 0, 0);
				border-radius: 50%;
				margin-left: 30rpx;
			}
			
			.name {
				font-size: 28rpx;
				color: #FFFFFF;
				// margin-left: 30rpx;
				flex: 1;
			}
			
			.item-name {
				color: #333333;
				font-size: 28rpx;
				margin-left: 30rpx;
				flex: 1;
				
				text {
					color: #FFFFFF;
				}
			}
			
			.steps {
				font-size: 36rpx;
				color: #FFFFFF;
			}
			
			.item-steps {
				font-size: 36rpx;
				color: #333;
			}
			
			.like {
				margin-left: 30rpx;
				
				.count {
					font-size: 28rpx;
					text-align: center;
					color: #C4CAD4;
					
				}
				
				.count.light {
					color: #FFFFFF;
				}
				
				.red {
					color: #E5404F !important;
				}
				
				.icon-box {
					.icon {
						width: 48rpx;
						height: 48rpx;
					}
				}
			}
		}
		
		.top-three-item {
			position: relative;
			
			.top-number {
				width: 80rpx;
				height: 90rpx;
				position: absolute;
				left: 0rpx;
				top: 0rpx;
				background-repeat: no-repeat;
				background-size: 100% auto;
				color: #FFFFFF;
				font-size: 28rpx;
				padding: 16rpx;
			}
		}
	}
	
	
</style>
